package com.atguigu.yygh.hosp.controller.api;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;

import lombok.extern.slf4j.Slf4j;

/** 
* @author 作者 Your-Name: 
* @version 创建时间：2021年5月31日 下午1:37:55 
* 类说明 
*/
@RestController
@Slf4j
@RequestMapping("/api/hosp")
public class ScheduleController {

	@Autowired
	private ScheduleService scheduleService;
	
	@Autowired
	private HospitalSetService hospitalSetService;
	
	@PostMapping("/saveSchedule")
	public Result saveSchedule(HttpServletRequest request){
		//获取到 传递过来的排班信息
		//返回   map 集合
		Map<String, String[]> requestMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
		// TODO 签名校验
		String hospitalSign = paramMap.get("sign").toString();
		String hostcode = paramMap.get("hostcode").toString();
		String signKey = hospitalSetService.getSignKey(hostcode);	
		String signKeyMD5 = MD5.encrypt(signKey);		
		if( !hospitalSign.equals(signKeyMD5) ){
			throw new YyghException(ResultCodeEnum.SIGN_ERROR);
		}
		scheduleService.save(paramMap);
		return Result.ok();
	}
	
	@PostMapping("/schedule/list")
	public Result findSchedule(HttpServletRequest request){
		//获取到 传递过来的排班信息
		//返回   map 集合
		Map<String, String[]> requestMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
		// TODO 签名校验
		String hospitalSign = paramMap.get("sign").toString();
		String hostcode = paramMap.get("hostcode").toString();
		//科室编号
		//String depcode = paramMap.get("depcode").toString();
		String signKey = hospitalSetService.getSignKey(hostcode);	
	    String signKeyMD5 = MD5.encrypt(signKey);		
		if( !hospitalSign.equals(signKeyMD5) ){
			throw new YyghException(ResultCodeEnum.SIGN_ERROR);
		}
		//当前页
		int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page").toString());
		//每页记录数   每页多少条数据
		int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt(paramMap.get("limit").toString());
		//封装查询条件
		ScheduleQueryVo scheduleQueryVo  = new ScheduleQueryVo();
		//scheduleQueryVo.setDepcode(depcode);
		scheduleQueryVo.setHostcode(hostcode);
		Page<Schedule> pageList = scheduleService.findSchedulePage(page, limit, scheduleQueryVo);
		return Result.ok(pageList);
	}
	
	@PostMapping("/schedule/remove")
	public Result removeSchedule(HttpServletRequest request){
		//获取到 传递过来的排班信息
		//返回   map 集合
		Map<String, String[]> requestMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
		// TODO 签名校验
		String hospitalSign = paramMap.get("sign").toString();
		String hostcode = paramMap.get("hostcode").toString();
		String hosScheduleId = paramMap.get("hosScheduleId").toString();
		String signKey = hospitalSetService.getSignKey(hostcode);	
		String signKeyMD5 = MD5.encrypt(signKey);		
		if( !hospitalSign.equals(signKeyMD5) ){
			throw new YyghException(ResultCodeEnum.SIGN_ERROR);
		}
		scheduleService.remove(hostcode, hosScheduleId);
		return Result.ok();
	}
}
